package com.cskaoyan.shopping.api;

import com.cskaoyan.mall.commons.result.ServiceCallResponse;
import com.cskaoyan.mall.commons.result.ServiceCallResponseUtil;
import com.cskaoyan.mall.dto.*;
import com.cskaoyan.shopping.converter.ProductConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.service.ICartService;
import com.cskaoyan.shopping.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @auther cskaoyan
 * @date 2022/8/24:14:56
 */
@RestController
public class OrderApiImpl {
    @Autowired
    IProductService productService;
    @Autowired
    ICartService iCartService;
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ProductConverter productConverter;


    @GetMapping(value = "/rpc/product/detail")
    ServiceCallResponse<ProductDetailDto> getProductDetail(@RequestParam("id") Long id) {
        ProductDetailDto productDetail = productService.getProductDetail(id);
        ServiceCallResponseUtil<ProductDetailDto> responseUtil = new ServiceCallResponseUtil<>();
        return responseUtil.ok(productDetail);
    }
    //TODO 调用接口删除购物车中的商品
    @DeleteMapping(value = "/rpc/cart/items")
    ClearCartItemServiceResponse clearCartItemByUserID(@RequestBody ClearCartItemRequest request){
        //根据商品的id删除购物车中的数据
        @NotEmpty List<Long> productIds = request.getProductIds();
        @NotNull Long userId = request.getUserId();

        for (Long productId : productIds) {
            iCartService.deleteCartItem(userId,productId);
        }
        return (ClearCartItemServiceResponse) new ServiceCallResponseUtil().ok(null);
    }

    /**
    * @description: search中的调用，获取所有的商品信息，插入elasticsearch的索引中
     *
    []
    * @return: com.cskaoyan.mall.dto.AllItemServiceResponse
    * @author: 杨庆
    * @time: 2022.9.27 19:14
     */
    @GetMapping("/rpc/items")
    AllItemServiceResponse getAllProductItem(){
        List<Item> items = itemMapper.selectAll();
        List<ItemDto> itemDtos = productConverter.items2ItemDtos(items);
        AllItemServiceResponse allItemServiceResponse = new AllItemServiceResponse();
        allItemServiceResponse.setCallData(itemDtos);
        return  allItemServiceResponse;
    }



}
